/*
 * @lc app=leetcode.cn id=98 lang=cpp
 *
 * [98] 验证二叉搜索树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    vector<int> ans;

    void dfs(TreeNode *root)
    {
        if (!root)
            return;
        dfs(root->left);
        ans.push_back(root->val);
        dfs(root->right);
    }

    bool isValidBST(TreeNode *root)
    {
        if (root == nullptr)
            return true;

        dfs(root);

        for (int i = 1; i < ans.size(); i++)
        {
            if (ans[i] <= ans[i - 1])
                return false;
        }
        return true;
    }
};

// @lc code=end
